package com.kde.crisisdepalabras;

import java.util.ArrayList;

import org.json.JSONArray;

import com.kde.crisisdepalabras.Model.Player;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PlayersSelection extends Activity implements TaskComplete
{
	 protected static final int REQUEST_CODE = 0;
 	 private ListView listViewPlayers;
	 private ArrayList<Player> players = new ArrayList<Player>();
	 private PlayersSelection p;
	 private GlobalClass globalVariable;
	    
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.playersselection);
       p = this;
       
       this.listViewPlayers = (ListView)findViewById(R.id.listViewPlayers);
       this.globalVariable = (GlobalClass)getApplicationContext();
       this.players =  this.globalVariable.getPlayers();
        //this.players = (ArrayList<Player>)getIntent().getSerializableExtra("players");
       
        if (this.players != null)
        {
	       PlayerAdapter listAdapter = new PlayerAdapter(this, R.layout.player, this.players);
	       this.listViewPlayers.setAdapter(listAdapter);
	       
	       this.listViewPlayers.setOnItemClickListener(new OnItemClickListener() 
		       {
	
				@Override
				public void onItemClick(AdapterView<?> arg0, View view, int position, long id) 
				{
					Player selectedFromList =(Player)(listViewPlayers.getItemAtPosition(position));
					String uidSelectedPlayer = selectedFromList.getId();	
	
					Toast.makeText(getApplicationContext(), "Selected Player: " + selectedFromList.getName(), Toast.LENGTH_SHORT).show();
					
					TextView v=(TextView) view.findViewById(R.id.txtName);
				    Toast.makeText(getApplicationContext(), "Selected Player: " + v.getText(), Toast.LENGTH_SHORT).show();				    				    
				    
				    ServiceTask service = new ServiceTask();
					service.setTaskComplete(p);
					service.execute("http://appeventscomar.fatcow.com/wsPreguntas/Duelo.php?Player1=" + globalVariable.getId() + "&Player2=" +uidSelectedPlayer);
					
					try
					{
						Intent quest = new Intent(p, QuestionActivity.class);
						startActivityForResult(quest, REQUEST_CODE);
						overridePendingTransition(R.anim.zoom_back_in, R.anim.zoom_back_out);
						//String myValue = quest.getStringExtra("Puntos"); 
						//Toast.makeText(getApplicationContext(), myValue, Toast.LENGTH_SHORT).show();
					}
					catch(Exception ex)
					{
						Log.e(getClass().getName(), ex.getMessage());
						Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();				    				    
					}
				}
		       });
	       
	         Toast.makeText(this, "SELECCIONE UN PARTICIPANTE", Toast.LENGTH_LONG).show();
        }
   }
   
   
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
	  if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
	    if (data.hasExtra("Question")) 
	    {
	    	Intent message;
	    	String puntos = data.getExtras().getString("Question");
	    	if (puntos != "0")
	    		message = new Intent(this, TransparentView.class);
	    	else
	    		message = new Intent(this, TransparentView.class);
	    	
	    	startActivity(message);
		    //ServiceTask service = new ServiceTask();
			//service.setTaskComplete(p);
			
			//hacer servicio que actualice puntaje
			//service.execute("http://appeventscomar.fatcow.com/wsPreguntas/Duelo.php?Player1=" + MainActivity.uid + "&Puntos=" + puntos);
	    }
	  }
	}

      
@Override
	public void onComplete(JSONArray result) 
	{
		// TODO Auto-generated method stub		
	}
}
